﻿using UnityEngine;
using System.Collections;

public class Stats : MonoBehaviour
{
    public enum State { Alive, Dead };
    public float Current_HP, Max_HP, Damage, Armor, AttackRange, ViewDistance;
    float DefaultCurrent_HP, DefaultMax_HP, DefaultDamage, DefaultArmor, DefaultViewDistance;
    public State currentState = State.Alive;
    public GameObject LogText;
    void Start() {
        
    }
    public void RecibeDamage(float damage)
    {
        GameObject go;
        if (damage - Armor <= 0)
        {
            Current_HP -= 1;
            go = Instantiate(LogText, transform.position + new Vector3(0, 2, 0), Quaternion.identity) as GameObject;
            go.GetComponentInChildren<TextMesh>().color = Color.white;
            go.GetComponentInChildren<TextMesh>().text = "" + (-1);
        }
        else
        {
            Current_HP -= damage - Armor;
            go = Instantiate(LogText, transform.position + new Vector3(0, 2, 0), Quaternion.identity) as GameObject;
            go.GetComponentInChildren<TextMesh>().color = Color.white;
            go.GetComponentInChildren<TextMesh>().text = "" + (-(damage-Armor));
        }
      
        if (Current_HP <= 0)
        {
            go = Instantiate(LogText, transform.position + new Vector3(0, 2, 0), Quaternion.identity) as GameObject;
            go.GetComponentInChildren<TextMesh>().color = Color.yellow;
            go.GetComponentInChildren<TextMesh>().text = "Dead";
            Current_HP = 0;
            currentState = State.Dead;
        }
    }
    public void RecibeDirectDamage(float damage)
    {
        Current_HP -= damage;
        if (Current_HP <= 0)
        {
            Current_HP = 0;
            currentState = State.Dead;
        }
    }
    public void Heal(int damage)
    {
        if (Current_HP + damage >= Max_HP)
        {
            Current_HP = Max_HP;
        }
        else
        {
            Current_HP += damage;
        }
    }
    public void InitStats() {
        DefaultArmor = Armor;
        DefaultCurrent_HP = Current_HP;
        DefaultDamage = Damage;
        DefaultMax_HP = Current_HP;
        DefaultViewDistance = ViewDistance;
    }
    public void CalculateStats() {
        Inventory inv= gameObject.GetComponent<Inventory>();
        Armor = DefaultArmor + inv.equipedArmor.armor;
        Damage = inv.equipedGun.Damage;
        Max_HP = DefaultMax_HP + inv.equipedArmor.HP;
        Current_HP = (DefaultCurrent_HP + inv.equipedArmor.HP) - ((DefaultCurrent_HP + inv.equipedArmor.HP)-Current_HP);
    }
}
